package com.airnewslinkosky.ticket.ui.personcenter;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.UserResponseResult;
import com.airnewslinkosky.ticket.net.WebAccessor;
import com.airnewslinkosky.ticket.pay.util.BaseHelper;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.ui.Main;
import com.airnewslinkosky.ticket.ui.PersonCenter;
import com.airnewslinkosky.ticket.ui.flight.FlyOrderInput;
import com.airnewslinkosky.ticket.ui.login.Registration;
import com.airnewslinkosky.ticket.ui.order.OrderList;
import com.mobclick.android.MobclickAgent;

/**
 * 密码修改
 * @author 
 */
public class UpdatePassword extends BaseActivity{
	private Button backBut,saveButton;
	private EditText oldPassword,newPassword;
	
	private ProgressDialog progressDialog = null;
	public Handler myHandler;
	private SharedPreferences sp;
	private Toast toast;
	
	public final int NORESULT = -1;
	public final int SAVE_ERROR = -2;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_password);
		activityList.add(this);
		
		sp = getSharedPreferences("UserInfoB2B", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
		
		oldPassword = (EditText)findViewById(R.id.oldPassword);
		newPassword = (EditText)findViewById(R.id.newPassword);
		
		OnClickListener b_ocl= new OnClickListener() { 
	    	public void onClick(View v) { 
	    		switch (v.getId()) {
	    		case R.id.backBut:
	    			Intent intent = new Intent();
	    			intent.setClass(UpdatePassword.this, PersonCenter.class);
	    			startActivity(intent);
	    			break;
	    		case R.id.saveButton:
	    			if ("".equals(oldPassword.getText().toString()) || "".equals(newPassword.getText().toString())){
	    				BaseHelper.showDialog(UpdatePassword.this, "提示", "请填写完整信息",
	    						R.drawable.infoicon);
	    				return;
	    			}
	    			save();
	    			break;
	    		}
	    	}
		};
		
		backBut = (Button) findViewById(R.id.backBut);
		backBut.setOnClickListener(b_ocl);
		
		saveButton = (Button) findViewById(R.id.saveButton);
		saveButton.setOnClickListener(b_ocl);
		
		myHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch (msg.what) {
				case NORESULT:
					toast = Toast.makeText(UpdatePassword.this, "未能成功修改密码", Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				case SAVE_ERROR:
					toast = Toast.makeText(UpdatePassword.this, (String)msg.obj, Toast.LENGTH_LONG);
					toast.setGravity(Gravity.CENTER, 0, 0);
					toast.show();
					break;
				}
			}
		};
	}

	private void save() {
		progressDialog = ProgressDialog.show(this, "请稍等...", "正在修改密码...",true);
		final Thread thread = new Thread() {
			public void run() {
		        try {
					String oldPasswordV = oldPassword.getText().toString();
					String newPasswordV = newPassword.getText().toString();
					
	    			
//					UserResponseResult result = WebAccessor.userRegister("",pwd,mobileV,serviceType);
					UserResponseResult result = null;
					System.out.println("Result = " + result.Result);
					if (progressDialog.isShowing()) {
						if (result== null) {
							Message msg = new Message();
							msg.what = NORESULT;
							myHandler.sendMessage(msg);
						}else if(!"0".equals(result.Result)){
							Message msg = new Message();
							msg.what = SAVE_ERROR;
							msg.obj = result.ErrorDescription;
							myHandler.sendMessage(msg);
						} else if("0".equals(result.Result)){
							//TODO修改成功处理
						}
						progressDialog.dismiss();
					}
		        } catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		progressDialog.setCancelable(true);
		thread.start();
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
	

}